package com.bs.redis.controller;

import com.alibaba.fastjson.JSONObject;
import com.bs.redis.vo.MessageVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author bingshao
 * @date 2023/3/7
 **/
@RestController
@RequestMapping("/pubsub")
public class PubSubController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/send/{channel}/{message}")
    public void send(@PathVariable("channel") String channel, @PathVariable("message") String message) {
        redisTemplate.convertAndSend(channel, message);
    }

    @PostMapping("/send/{topic}")
    public void send(@PathVariable("topic") String topic, @RequestBody MessageVo messageVo) {
        redisTemplate.convertAndSend(topic, JSONObject.toJSONString(messageVo));
    }

}
